package com.sciencep.pro.core.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

import java.time.LocalDate;
import java.util.*;

@Slf4j
public class UserUtil {

    private UserUtil() {

    }

    /**
     * 判断该日期是否为节假日
     *
     * @param date 1,正常工作日  2,节假日 3,节假日后补班 4,周末
     * @return
     */
    public static List<Integer> getIfHoliday(String date) {
        Integer flag = 1;
        Integer week = 1;
        String holiday = "holiday";
        List<Integer> list = new ArrayList<>();
        try {
            RestTemplate restTemplate = new RestTemplate();
            HttpHeaders headers = new HttpHeaders();
            headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
            headers.add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.99 Safari/537.36");
            HttpEntity<String> entity = new HttpEntity<>(headers);
            Map<String, Object> map = new HashMap<>();
            ResponseEntity<Object> response = restTemplate.exchange("http://timor.tech/api/holiday/info/" + date, HttpMethod.GET, entity, Object.class);
            if (response.hasBody()) {
                map = (Map<String, Object>) response.getBody();
                if (map == null) {
                    map = new HashMap<>();
                }
            }

            if (map.get(holiday) == null) {
                flag = 1;
            } else {
                Map<String, Object> holidayDO = (Map) map.get(holiday);
                Boolean object = (Boolean) holidayDO.get(holiday);
                if (object.equals(true)) {
                    flag = 2;//节假日
                } else {
                    flag = 3;//节假日补班
                }
            }
            LocalDate localDate = LocalDate.parse(date);
            int value = localDate.getDayOfWeek().getValue();
            if ((value == 6 || value == 7) && flag != 3) { //如果不是节假日补班情况，则返回周末
                flag = 4;//周末
            }
            list.add(flag);
            //星期几
            if (map.get("type") == null) {
                week = 1;
            } else {
                Map<String, Object> holidayDO = (Map) map.get("type");
                int object = (int) holidayDO.get("week");
                week = object;
            }
            list.add(week);
        } catch (Exception e) {
            e.printStackTrace();
            log.info("获取是否工作日异常.", e);
        }
        return list;
    }

}
